﻿using System;
using System.IO;
using System.Reflection;

namespace TeF.Runner.ConsoleUI
{
    public class Program
    {
        public static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;

            Console.WriteLine("TeF console test runner version {0}.", Assembly.GetExecutingAssembly().GetName().Version);
            
            if (args.Length == 0 || args[0] == "/?")
            {
                WriteHelp();
                return;
            }
              
            string[] assemblyPathes = null;
            if (args.Length > 0)
                assemblyPathes = args[0].Split(';');

            string reportDirectoryPath = 
                args.Length > 1 
                    ? args[1] :
                    Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            var manager = ManagerFactory.Create();
            var resultFilePath = manager.Run(assemblyPathes, reportDirectoryPath);

            Console.WriteLine("Result file can be found at '{0}'", resultFilePath);
        }

        private static void WriteHelp()
        {
            Console.WriteLine("Arguments:");
            Console.WriteLine(@"1st argument (mandatory) - list of pathes to test assemblies separated by semicolon ';'. Example: 'c:\assembly.dll;d:\assembly.dll'");
            Console.WriteLine(@"1nd argument (optional) - directory path for test reports. Example: 'c:\ReportDir'");
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.ExceptionObject.ToString());

            Environment.Exit(1);
        }
    }
}
